from whiff import resolver
from whiff.middleware.wrapIfHtml import wrapIfHtml

class header_trailer_wrapper:
    def __init__(self, page):
        self.page = page

    def __call__(self, env, start_response):
        page = self.page
        # need to use list() here to make sure the start_response is called before first yield (?)
        #pr "header_trailer calling page", page
        payload = list(page(env, start_response))
        payload.insert(0, "<br>[header from header_trailer.py]<hr>\n")
        payload.append("<hr>[trailer from header_trailer.py]<br>\n")
        return payload

def wrap_html_with_header_trailer(page):
    #pr "calling wrapIfHtml", (header_trailer_wrapper, page)
    return wrapIfHtml(header_trailer_wrapper, page)

__middleware__ = wrap_html_with_header_trailer
